﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using YouHP.Model;
using YouHP.Model.Group;
using YouHP.BLL;
using YouHP.Utility;

namespace web.admin
{
    public partial class GroupEdit : System.Web.UI.Page
    {
        private GroupBLL groupBll = GroupBLL.Instance;
        private GroupTypeBLL gtBll = GroupTypeBLL.Instance;
        private string URL_BACK = "Group.aspx";
        private int selTypeId = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        private void Bind()
        {
            try
            {
                int id = RequestTools.GetQueryInt("id", -1);
                if (id > 0)
                {
                    IList list = new ArrayList();
                    GroupInfo info = groupBll.GetGroupInfo(id);
                    if (info != null)
                    {
                        this.selTypeId = info.GroupType;
                        string[] moderatorStrArr = info.Moderators.Split('@');

                        info.Moderators = "";
                        for (int i = 0; i < moderatorStrArr.Length; i++)
                        {
                            if (i > 0)
                            {
                                info.Moderators += ";";
                            }

                            info.Moderators += moderatorStrArr[i].Substring(0, moderatorStrArr[i].IndexOf(';'));
                        } 
                        
                        list.Add(info);
                    } 

                    this.groupInfo.DataSource = list;
                    this.groupInfo.DataBind();
                }
            }
            catch
            {
                //重定向到出错页面
            }
        }

        protected void typeListBound(object sender, EventArgs e)
        {
            try
            {
                //绑定类型列表值
                DropDownList list = (DropDownList)(((DetailsView)sender).FindControl("typeList"));
                list.DataSource = gtBll.GetAllGroupType();
                list.DataTextField = "GroupTypeName";
                list.DataValueField = "Id";
                list.DataBind();

                list.SelectedValue = "" + this.selTypeId;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void UpdateButton_click(object sender, EventArgs e)
        {
            //数据有效性检查
            try
            {
                GroupInfo info = validateForm(this.groupInfo);
                if (info != null)
                {
                    info.Id = Convert.ToInt32(this.groupInfo.DataKey.Value);

                    //准备数据,调用SqlServerDAL执行更新
                    if (groupBll.UpdateGroup(info))  //成功
                    {
                        HttpContext.Current.Response.Redirect(URL_BACK, true);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void CancelButton_click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Redirect(URL_BACK, true);
        }

        //数据有效性检查
        private GroupInfo validateForm(DetailsView formViewCtl)
        {
            GroupInfo info = new GroupInfo();

            info.GroupName = ((TextBox)formViewCtl.FindControl("GroupName")).Text;
            info.GroupType = Convert.ToInt32(((DropDownList)formViewCtl.FindControl("typeList")).SelectedValue);
            info.GroupTypeName = ((DropDownList)formViewCtl.FindControl("typeList")).Items[
                ((DropDownList)formViewCtl.FindControl("typeList")).SelectedIndex].Text;
            info.Introduction = ((TextBox)formViewCtl.FindControl("Introduction")).Text;
            info.Logo = ((TextBox)formViewCtl.FindControl("Logo")).Text;

            info.TopicSortString = ((TextBox)formViewCtl.FindControl("TopicSortString")).Text;
            string[] SortArray=info.TopicSortString.Split('@');
            if (SortArray.Length > 7)
            {
                this.errorMsg.Text = "帖子类别不能超过为7种！";
                this.errorMsg.Visible = true;

                return null;
            }
            

            string moderators = ((TextBox)formViewCtl.FindControl("Moderators")).Text;
            string[] moderatorArr = moderators.Split(';');
            int tmpUserId = -1;

            if (moderatorArr.Length > 5)
            {
                this.errorMsg.Text = "版主设置不能超过5个！";
                this.errorMsg.Visible = true;

                return null;
            }

            for (int i = 0; i < moderatorArr.Length; i++)
            {
                tmpUserId = UserBLL.Instance.GetUserIdByUserName(moderatorArr[i]);
                if (tmpUserId > 0)
                {
                    if (i > 0)
                    {
                        info.Moderators += "@";
                    }
                    info.Moderators += moderatorArr[i] + ";" + tmpUserId;
                }
                else
                {
                    this.errorMsg.Text = "版主不存在！";
                    this.errorMsg.Visible = true;

                    return null;
                }
            }
                
            return info;
        }
    }
}
